package sdkshell.kuaiyan.com.kuaiyansdk.util;

import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;

import sdkshell.kuaiyan.com.kuaiyansdk.SdkInit;


/**
 * Created by Max on 2016/6/23.
 */
public class ToastUtils {
    private static Toast toast;
    public static boolean isDebug = true;
    public static void showAtLocation(Context context, String msg, int x, int y){
        cancel();
        if(toast == null){
            toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
            toast.setMargin(x,y);
        }
        toast.show();
    }

    public static void showDefault(Context context, String msg){
        cancel();
        if(toast == null){
            toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        }
        toast.show();
    }

    public static void showCenter(Context context, String msg){
        cancel();
        if(toast == null){
            toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
    }
    public static void cancel(){
        if(toast!=null){
            toast.cancel();
            toast = null;
        }
    }

    public static void showDefaultSafe(final Context context, final String msg){
        SdkInit.getUiHandle().post(new Runnable() {
            @Override
            public void run() {
                showDefault(context,msg);
            }
        });
    }

    public static void showCenterSafe(final Context context, final String msg){
        SdkInit.getUiHandle().post(new Runnable() {
            @Override
            public void run() {
                showCenter(context,msg);
            }
        });
    }

    public static void showAtLocationSafe(final Context context, final String msg, final int x, final int y){
        SdkInit.getUiHandle().post(new Runnable() {
            @Override
            public void run() {
                showAtLocation(context,msg,x,y);
            }
        });
    }



    public static void showAtLocationDebug(Context context, String msg, int x, int y){
        if(isDebug) {
            cancel();
            if (toast == null) {
                toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
                toast.setMargin(x, y);
            }
            toast.show();
        }
    }

    public static void showDefaultDebug(Context context, String msg){
        if(isDebug) {
            cancel();
            if (toast == null) {
                toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
            }
            toast.show();
        }
    }

    public static void showCenterDebug(Context context, String msg){
        if(isDebug) {
            cancel();
            if (toast == null) {
                toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
            }
            toast.show();
        }
    }

    public static void showDefaultSafeDebug(final Context context, final String msg){
        if(isDebug) {
            SdkInit.getUiHandle().post(new Runnable() {
                @Override
                public void run() {
                    showDefault(context, msg);
                }
            });
        }
    }

    public static void showCenterSafeDebug(final Context context, final String msg){
        if(isDebug) {
            SdkInit.getUiHandle().post(new Runnable() {
                @Override
                public void run() {
                    showCenter(context, msg);
                }
            });
        }
    }

    public static void showAtLocationSafeDebug(final Context context, final String msg, final int x, final int y){
        if(isDebug) {
            SdkInit.getUiHandle().post(new Runnable() {
                @Override
                public void run() {
                    showAtLocation(context, msg, x, y);
                }
            });
        }
    }
}
